import * as THREE from 'three';
import SelectMeshConfig from "./select-mesh-config";
import GeometryUtils from "../general/geometry-utils";

/**
 * 选取Object3D的配置
 */
export default class SelectObjectConfig extends SelectMeshConfig{
    constructor(values={}){
        super(values);
        console.warn('this class is deprecated now ,use new SelectMeshGroupConfig which also support XR!');
    }
    get type(){
        return 'SelectObjectConfig';
    }
    checkType(object) {
        return object instanceof THREE.Object3D;
    }

    /**
     * 检测所有子级（含自身）的投影几何体
     */
    intersectRect(object,pointer1,pointer2,camera,rect){
        if(!object.visible)return false;
        let box=this._tB0;
        GeometryUtils.make2DAABB(pointer1.x,pointer2.x,pointer1.y,pointer2.y,box,false);
        box.min.z=-0.1;
        box.max.z=0.1;
        let sus=false;
        object.traverse((c)=>{
            if(!sus){
                if(c instanceof THREE.Mesh){
                    sus=this.checkProjectionGeometry(c,box,camera,rect);
                    if(sus)return true;
                }
            }
        });
        return sus;
    }

    /**
     * 将射线检测所得对象（mesh）替换为被检测集合（object3D）
     */
    intersectPointer(object,pointer,info,camera,rect){
        if(!object.visible)return false;
        let x = (pointer.x - rect.left) / rect.width;
        let y = (pointer.y - rect.top) / rect.height;
        this._tV0.set(x * 2 - 1, 1 - y * 2,0);
        this._rayCaster.setFromCamera(this._tV0, camera);
        this.tA0.length=0;
        this._rayCaster.intersectObject(object, true,this.tA0);
        for(let i of this.tA0){
            i.object=object;
            i.object.userData.intersectDetail=i;
        }
        info.push(...this.tA0);
        return info;
    }

    dispose() {
        super.dispose();
        this.tA0=null;
    }

    _initVariable() {
        super._initVariable();
        this.tA0=[];
    }
}